package sy.util;


import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

public class EmailUtil {
	//主要利用message.setContent(content, "text/html;charset=utf-8");来实现发送html内容的邮件
	//private static final   String host = "smtp.sina.com"; //sina
//	private static final   String host = "smtp.mxhichina.com";//阿里云
	private static final   String host = "smtp.mxhichina.com";
	//private static final     String user = "xtdx888@sina.com";

	//private static final     String user = "sinotrans@jcmos.com";
//	private static final    String password = "sinotrans001";
	private static final    String password = "sinotrans001";
	//拒收正式环境测试环境承运商责任赔偿单 id611767
	//demo 	var url = "http://reportserver.sinotrans.com:7003/ReportManage/ReportServer?reportlet=com.sinotrans.reportserver.ReadFromDB&rsrtId=611758;
	
	public static final String rejectionURl="http://222.72.139.13:8888/WebReport/ReportServer?reportlet=rejection.cpt";
	public static final String reportURl="http://reportserver.sinotrans.com:7003/ReportManage/ReportServer?reportlet=com.sinotrans.reportserver.ReadFromDB";
	
	//退货装车单
	public static final String returnedPurchaseURl="http://222.72.139.13:8888/WebReport/ReportServer?reportlet=returnedPurchase.cpt";

	//拒收bbxd待提货单
	public static final String bbrejectionURl="http://222.72.139.13:8888/WebReport/ReportServer?reportlet=bbrejection.cpt";
	//转仓单待提货单
	public static final String transferOrderURl="http://222.72.139.13:8888/WebReport/ReportServer?reportlet=transferOrder.cpt";

	private static final String path = null;
	
/*	    public void setHost(String host) {
	        this.host = host;
	    }
	    public void setAccount(String user, String password) {
	        this.user = user;
	        this.password = password;
	    }*/
	    //发送单个邮件
	    public static void send(String from, String to, String subject, String content) {
	        Properties props = new Properties();

	        props.put("mail.smtp.host", host); // 指定SMTP服务器
	        props.put("mail.smtp.auth", "true"); // 指定是否需要SMTP验证
	        try {
	            Session mailSession = Session.getDefaultInstance(props);
	            Message message = new MimeMessage(mailSession);
	           // message.setFrom(new InternetAddress(from)); // 发件人
	            // message.setFrom(new InternetAddress("xtdx888@sina.com")); // 发件人
	            message.setFrom(new InternetAddress(from)); // 发件人
	            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 收件人
	            message.setSubject(subject); // 邮件主题
	            //指定邮箱内容及ContentType和编码方式
	            message.setContent(content, "text/html;charset=utf-8");
	            //指定邮件发送日期
	            message.setSentDate(new Date());
	            message.saveChanges();
	            Transport transport = mailSession.getTransport("smtp");
	           // transport.connect(host, user, password);
	            transport.connect(host, "htwljinbaili", password);
	            transport.sendMessage(message, message.getAllRecipients());
	            transport.close();
	        } catch(Exception e) {
	           e.printStackTrace();
	        }

	    }
	    //批量发送邮件
		public static void send(String from,List<String> recipients, String subject, Object content,String filepath)
				throws AddressException, MessagingException {
			Properties props = new Properties();
			props.put("mail.smtp.host", host); // 指定SMTP服务器
			props.put("mail.smtp.auth", "true"); // 指定是否需要SMTP验证
			Session mailSession = Session.getDefaultInstance(props);
			Message message = new MimeMessage(mailSession);
			//   message.setFrom(new InternetAddress(from)); // 发件人
			message.setFrom(new InternetAddress(from)); // 发件人
			//    message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 收件人
			message.setSubject(subject); // 邮件主题
			//指定邮箱内容及ContentType和编码方式
			//  message.setContent(content, "text/html;charset=utf-8");
			//指定邮件发送日期
			message.setSentDate(new Date());
			//message.setFileName(arg0);

			// 向multipart对象中添加邮件的各个部分内容，包括文本内容和附件
			Multipart multipart = new MimeMultipart();


			// 添加邮件正文
			BodyPart contentPart = new MimeBodyPart();
			contentPart.setContent(content.toString(), "text/html;charset=utf-8");
			multipart.addBodyPart(contentPart);
			// 设置邮件内容

			// 添加附件的内容
			if (filepath != null&&!filepath.equals("")) {
				File file=new File(filepath);
				BodyPart attachmentBodyPart = new MimeBodyPart();
				DataSource source = new FileDataSource(file);
				attachmentBodyPart.setDataHandler(new DataHandler(source));

				// 网上流传的解决文件名乱码的方法，其实用MimeUtility.encodeWord就可以很方便的搞定
				// 这里很重要，通过下面的Base64编码的转换可以保证你的中文附件标题名在发送时不会变成乱码
				//sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
				//messageBodyPart.setFileName("=?GBK?B?" + enc.encode(attachment.getName().getBytes()) + "?=");

				//MimeUtility.encodeWord可以避免文件名乱码
				try {
					attachmentBodyPart.setFileName(MimeUtility.encodeWord(file.getName()));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				multipart.addBodyPart(attachmentBodyPart);
			}

			// 将multipart对象放到message中
			message.setContent(multipart);




			// 设置收件人们
			final int num = recipients.size();
			InternetAddress[] addresses = new InternetAddress[num];
			for (int i = 0; i <num; i++) {
				addresses[i] = new InternetAddress(recipients.get(i));
			}
			message.setRecipients(RecipientType.TO, addresses);
			// 设置主题

			message.saveChanges();
			// 发送
			Transport transport = mailSession.getTransport("smtp");

			transport.connect(host, from, password);
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();
		}
	    
	    
	    
	    
	    public static void main(String[] args) {

		}
}
